package com.zjitc.backend_shop.security;

import com.zjitc.backend_shop.mapper.AdminMapper;
import com.zjitc.backend_shop.pojo.Admin;
import com.zjitc.backend_shop.pojo.po.LoginUser;
import com.zjitc.backend_shop.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*
    @author： 尤安托
    @date： 2025/4/16 14:34
*/
@Service
public class MyUserDetailServiceImpl implements UserDetailsService {
//    @Autowired
//    AdminService adminService;
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        Admin byUsername = adminService.findByUsername(username);
//        if(byUsername==null){
//            throw new  UsernameNotFoundException("不存在此用户");
//        }
//        return new LoginUser(byUsername);
//    }
    @Autowired
    AdminMapper adminMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Example example = new Example(Admin.class);
        example.createCriteria().andEqualTo("username", username);
        List<Admin> admin = adminMapper.selectByExample(example);
        if (admin.isEmpty()) {
            throw new  UsernameNotFoundException("不存在此用户");
        }
        //赋权
        List<String> list=new ArrayList<>();
        list.add("select");
        list.add("delete");
        list.add("ROLE_ADMIN");
        return new LoginUser(admin.get(0),list);
    }
}
